﻿(function () {
    if (!HTMLInputElement.prototype.setSelectionRange) {
        HTMLInputElement.prototype.setSelectionRange = function(start, end) {
            if (this.createTextRange) {
                var range = this.createTextRange();
                range.collapse(true);
                range.moveEnd('character', end);
                range.moveStart('character', start);
                range.select();
            }
        };
    }
})();

jQuery.fn.setSelection = function(start, end) {
    if (this.lengh == 0) return this;
    var input = this[0];
    //input.focus();
    input.setSelectionRange(start, end);
    return this;
};

jQuery.fn.carotToEnd = function () {
    var len = this.val().length;
    this.setSelection(len, len);
};

jQuery.fn.getStyles = function() {
    var dom = this.get(0);
    var style;
    var returns = { };
    if (window.getComputedStyle) {
        //var camelize = function(a, b) {
        //    return b.toUpperCase();
        //};
        style = window.getComputedStyle(dom, null);
        for (var i = 0, l = style.length; i < l; i++) {
            var prop = style[i];
            if (prop.indexOf('-webkit') == 0) // Filter out -webkit stuff because it makes the element disappear
                continue;
            //var camel = prop.replace( /\-([a-z])/g , camelize);
            var val = style.getPropertyValue(prop);
            returns[prop] = val; // This used to use camel instead of prop (for some reason)
        }
        ;
        return returns;
    }
    ;
    if (style = dom.currentStyle) {
        for (var prop in style) {
            returns[prop] = style[prop];
        }
        ;
        return returns;
    }
    ;
    if (style = dom.style) {
        for (var prop in style) {
            if (typeof style[prop] != 'function') {
                returns[prop] = style[prop];
            }
            ;
        }
        ;
        return returns;
    }
    ;
    return returns;
};
